const express = require("express")
const router = express.Router();

//导入模型 m
const myQueryPromise = require("../model/queryPromise.js");

//获取所用的分类
router.get("/cate",async(req,res)=>{
    let sql = "select * from mycategories where isshow = 1";
    let result =await myQueryPromise(sql)
    res.json(result)
})
//获取文章详情
router.get("/detail",async(req,res)=>{
 
    let {id}= req.query
    id= parseInt(id)
    id=isNaN(parseInt(id)) ? 0 : parseInt(id);
    let sql = `select t1.*,t2.name from mypost t1 left join mycategories t2 on t1.cat_id =t2.id  where t1.id= ${id}`;
    let result =await myQueryPromise(sql)
    res.json(result[0] || {})
})
//获取首页分页文章
router.get("/article",async(req,res)=>{
    let {page = 1, pagesize = 3 } = req.query;
    //起始位：（当前页-1） *pagesize；
    let offset = (page - 1) *pagesize;
    let sql = ` select t1.*,t2.name from mypost t1 left join mycategories t2
                on t1.cat_id =t2.id where t1.is_delete =0 and t1.state limit ${offset}, ${pagesize} `
                let result = await myQueryPromise(sql);
                res.json(result);
})
//获取分类页面的文章
router.get("/categorical",async(req,res)=>{
    let {page = 1,pagesize=2,cat_id} = req.query;
    let offset = (page -1)*pagesize;
        let sql =`select t1.*, t2.name from mypost t1 left join mycategories t2 on t1.cat_id = t2.id where t1.cat_id = ${cat_id} 
             and t1.is_delete = 0 and t1.state=1 limit ${offset},${pagesize}` 
             let result = await myQueryPromise(sql)
             res.json(result)
})
router.get("*",(req,res)=>{
    res.json({message:"404"})
})
module.exports = router;